#include "solver.h"

string CSolver::MaGroupName="MaG";
string CSolver::MbGroupName="MbG";

CSolver::CSolver()
:CHDF5GroupObject()
{
	Ma=new Matrix(2,3,1.0);
	Mb=new Matrix(2,3,2.0);
}

CSolver::~CSolver()
{
	delete Ma;
	delete Mb;
}

int CSolver::write(const H5std_string &filename, const H5std_string &location, const H5std_string &groupName)
{
	int res=0;

	try
	{
		Exception::dontPrint();

		if (h5_file==NULL || h5_file->getFileName()!=filename)
		{
			h5_file=new H5File(filename,H5F_ACC_TRUNC);
		}
		
		if(createGroups(location,groupName))
		{
			Ma->h5_file=h5_file;
			Ma->write(filename,location+"/"+groupName,MaGroupName);

			Mb->h5_file=h5_file;
			Mb->write(filename,location+"/"+groupName,MbGroupName);
			res=1;
		}

	}
	// catch failure caused by the H5File operations
	catch( FileIException error )
	{
		
		errMsg=error.getDetailMsg();
	}
	return res;
}

int CSolver::append(const H5std_string &filename, const H5std_string &location, const H5std_string &groupName)
{
	int res=0;

	try
	{
		Exception::dontPrint();

		if (h5_file==NULL || h5_file->getFileName()!=filename)
		{
			h5_file=new H5File(filename,H5F_ACC_RDWR);
		}

		H5std_string path=location+"/"+groupName;

		if (isGroupExist(path))
		{
			Ma->h5_file=h5_file;
			Ma->append(filename,path,MaGroupName);

			Mb->h5_file=h5_file;
			Mb->append(filename,path,MbGroupName);
		}

		res=1;
	}
	// catch failure caused by the H5File operations
	catch( FileIException error )
	{
		errMsg=error.getDetailMsg();
		//errMsg=error.getDetailMsg();
	}
	return res;
}

int CSolver::read(const H5std_string& filename,const H5std_string& location,const H5std_string& groupName)
{
	int res=0;

	try
	{
		Exception::dontPrint();

		if (h5_file==NULL || h5_file->getFileName()!=filename)
		{
			h5_file=new H5File(filename,H5F_ACC_RDONLY);
		}

		H5std_string groupPath=location+"/"+groupName;
		if (isGroupExist(groupPath))
		{
			Ma->read(filename,groupPath,MaGroupName);
			Mb->read(filename,groupPath,MbGroupName);
			res=1;
		}
	}
	// catch failure caused by the H5File operations
	catch( FileIException error )
	{
		errMsg=error.getDetailMsg();
	}
	return res;
}